define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
         method = app.method;
    var $putaway = $('[data-id="putaway"]');
    var item_tmp ="",classes="",page_tmp = require("./template/ajaxPage.html");
    if(window.freeship_data.type && window.freeship_data.type=="distribution"){
        item_tmp=require("./template/goodsajaxdistribution.html");
    }else if(window.freeship_data.type && window.freeship_data.type=="vke"){
        item_tmp=require("./template/goodsajaxvke.html");
    }else if(window.freeship_data.type && window.freeship_data.type=="newest"){
        item_tmp=require("./template/goodsajaxnewest.html");
        page_tmp= require("./template/ajaxPageNew.html")
        classes="newest-selected";
    }else{
        item_tmp=require("./template/goodsajax.html");
    }
    var $goods_show=$("#goods_show");
    var tdata=$("#putaway form").serialize();
    var $goodslist=$("#goods_select_input");
    var $js_goods_count=$(".js_goods_count");
    $putaway.length>0&&loadGoods($putaway.data("path"),tdata);
    $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
      $goods_show.html('<i class="fa fa-spinner fa-spin"></i>');
    })
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
       var $this=$(e.target),path=$this.data("path"),id=$this.data("id"),
       tdata=$("#"+id+" form").serialize(),$target=$('div.tab-pane.active'),$page=$(".js_page_goods",$target);
       if(id=="selected"){
            tdata+="&ids="+window.freeship_data.goods+"";
            $goods_show.addClass(classes);
            loadGoods(path,tdata);
       }else{
            $goods_show.removeClass(classes);
            loadGoods(path,tdata);
       }
    })

    function loadGoods(path,tdata){
        method.post(path, function(data) {
            if (config.issucceed(data)) {
                var putaway=data.data.data,pagelist=[],total=data.data.totalPage,current=data.data.currentPage;
                if(putaway){
                    for(var i=0,len=putaway.length;i<len;i++){
                        if($.inArray(putaway[i].id, window.freeship_data.goods)>-1){
                            putaway[i].checked=true;
                        }
                    }
                    if(total>0){
                        if(total-current>=2){
                            var start=1,end=5;
                            start=(current-2)>0?(current-2):1;
                            end=start+4;
                            for(var i=start;i<=end;i++){
                                if(i>0&&i<=total){
                                    pagelist.push(i);
                                }
                            }
                        }else{
                            var start=1,end=5;
                            end=(current+2)<=total?(current+2):total;
                            start=end-4;
                            for(var i=start;i<=end;i++){
                                if(i>0&&i<=total){
                                    pagelist.push(i);
                                }
                            }
                        }
                        
                    }
                }
                data.data.pagelist=pagelist;
                data.data.selected=$goods_show.hasClass(classes);
                var html=template.compile(item_tmp+page_tmp)(data.data);
                $goods_show.html(html);
                if(!$goods_show.hasClass(classes)){
                    var len=$(".list-goods li",$goods_show).length,select=$(".list-goods li.selected",$goods_show).length,
                    disabled=$(".list-goods li.disabled",$goods_show).length;
                   if(len==(select+disabled)){
                        $(".js_page_goods",$goods_show).prop("checked",true);
                   }
                }
            }else{
                config.msg.error(data.message);
            }
        }, config.lang.exception,false,tdata);
    }
    $(document).on("click",".list-goods li",function(){
        var $this=$(this),flag=$this.hasClass("selected"),disabled=$this.hasClass("disabled"),id=$this.data("id")+"",
       $target=$('div.tab-pane.active'),$page=$(".js_page_goods",$target);
        if(!disabled){
            if(flag){
                $this.removeClass("selected");
                $page.prop("checked",false);
                if($goods_show.hasClass(classes)){
                   // $this.remove();
                }
                getData(id,window.freeship_data.goods,true);
            }else{
                $this.addClass("selected");
                getData(id,window.freeship_data.goods,false);
            }
        }
        
    });
    function getData(id,arr,flag){
        if(flag){
            var index=$.inArray(id, arr);
            if(index>-1){
                arr.splice(index,1);
            }
        }else{
            arr.push(id);
        }
        if(arr.length>0){
            $goodslist.length>0&&$goodslist.val(window.freeship_data.goods);
            $js_goods_count.length>0&&$js_goods_count.html(arr.length);
        }else{
            $goodslist.length>0&&$goodslist.val("");
            $js_goods_count.length>0&&$js_goods_count.html(0);
        }
    }
    $(document).on("click",".js_page_goods",function(){
        var $this=$(this),checked=$this.prop("checked"),
        $list=$goods_show.find(".list-goods li");
        if(checked){
            $list.each(function(){
                var _this=$(this),id=_this.data("id")+"",disabled=_this.hasClass("disabled");
                if(!disabled&&!_this.hasClass("selected")){
                    _this.addClass("selected");
                    getData(id,window.freeship_data.goods,false);
                }
            });
        }else{
            $list.each(function(){
                var _this=$(this),id=_this.data("id")+"",disabled=_this.hasClass("disabled");
                if(!disabled&&_this.hasClass("selected")){
                    _this.removeClass("selected");
                    getData(id,window.freeship_data.goods,true);
                }
            });
        }

    });
    $(document).on("click","button[data-type='order']",function(){
        var $this=$(this),$form=$this.closest('form'),$order=$($this.data("order"),$form),
        $orderfield=$($this.data("orderField"),$form),orderv=$order.val(),orderfieldv=$orderfield.val(),
        field=$this.data("field"),$btns=$("button[data-type='order']",$form),$icons=$btns.find("i"),
        $icon=$this.find("i"),path=$form.data("path"),$page=$(".js_page_goods",$form),$pages=$(".js_page",$form);
        $btns.removeClass('btn-danger');
        $icons.addClass('fa-arrows-v').removeClass('fa-long-arrow-down fa-long-arrow-up')
        $orderfield.val(field);
        $pages.val(1);
        $page.prop("checked",false);
        $this.addClass('btn-danger');
        switch(orderv){
            case "desc":$order.val("asc");
                $icon.addClass('fa-long-arrow-up').removeClass('fa-arrows-v');
                break;
            case "asc":$order.val("desc");
                $icon.addClass('fa-long-arrow-down').removeClass('fa-arrows-v');
                break;
            default:$order.val("desc");
                    $icon.addClass('fa-long-arrow-down').removeClass('fa-arrows-v');
                break;
        }
        var tdata=$form.serialize();
        loadGoods(path,tdata);
    });
    $(document).on("click","button[data-type='ajaxSearch']",function(){
         var $this=$(this),$form=$this.closest('form'),path=$form.data("path"),
         $page=$(".js_page_goods",$form),$pages=$(".js_page",$form);
         $pages.val(1);
         $page.prop("checked",false);
         var tdata=$form.serialize();
         loadGoods(path,tdata);
    });
    $(document).on("change","select[data-type='ajaxSelectSearch']",function(){
         var $this=$(this),$form=$this.closest('form'),path=$form.data("path"),
         $page=$(".js_page_goods",$form),$pages=$(".js_page",$form);
         $pages.val(1);
         $page.prop("checked",false);
         if($this.hasClass('js_search_select_type_select')){
            var type=$this.find("option:selected").data("type");
            $(".js_search_select_type",$form).addClass('hide').prop("disabled",true).find("option").removeAttr('selected');
            $(".js_search_select_type_"+type,$form).removeClass('hide').prop("disabled",false);
         }
         var tdata=$form.serialize();
         loadGoods(path,tdata);
    });  
    $(document).on("click","footer[data-type='ajaxPage'] a",function(){
        var $this=$(this),$li=$this.closest('li'),flag=$li.hasClass('disabled'),
        active=$li.hasClass('active'),$tab=$("div.tab-pane.active"),id=$tab.attr("id"),
        $form=$tab.find("form"),path=$form.data("path"),$pageselect=$(".js_page_goods",$form),
        $page=$(".js_page",$form),pagenum=$this.data("pageNum");
        if(!flag&&!active){
            $page.val(pagenum);
            $("footer[data-type='ajaxPage'] li.active").removeClass("active");
            $li.addClass('active');
            $pageselect.prop("checked",false);
            var tdata=$form.serialize();
            if(id=="selected"){
                tdata+="&ids="+window.freeship_data.goods+"";
            }
            loadGoods(path,tdata);
        }
    });
    $(document).on("click",".js_goods_submit_btn",function(){
        if($goodslist.length>0&&$goodslist.val().length<1){
            config.msg.error("请选择商品");
            return false;
        }
    }); 
    $(document).on("click",".js_goods_submit_part",function(){
        if($goodslist.length>0&&$goodslist.val().length<1&&!$(".js_party_goods").hasClass("hide")){
            config.msg.error("请选择商品");
            return false;
        }
    }); 
    $(document).on("keypress", function(e) {
        var e = e || event;　
        var currKey = e.keyCode || e.which || e.charCode;
        if (13 == currKey) {
            $("div.tab-pane.active button[data-type='ajaxSearch']").trigger('click');
            return false;
        }


    }) 
    $(document).on("click",".js_goods_type_select input[type='radio']",function(){
        typegood($(this));
    });
    var $gtype=$(".js_goods_type_select input[type='radio']:checked");
    $gtype.length && (function(){
        typegood($gtype);
    })();
    $(document).on("click",".js_goods_type_select input[type='checkbox']",function(){
        typegoodnew($(this));
    });
    var $gtype=$(".js_goods_type_select input[type='checkbox']");
    $gtype.length && (function(){
        typegoodnew($gtype);
    })();
    function typegoodnew(obj){
        var $type=$("#goods_select_type"),type=obj.val(),
        type1=obj.data("nochecked"),flag=obj.prop("checked"),
        $js_type_goods_div=$(".js_type_goods_div"),$p=obj.closest('.nav-tabs'),
        $js_goods_count_part=$(".js_goods_count_part");
        if(!flag){
            type=type1;
            $js_goods_count_part.removeClass('hide');
            $('a[data-toggle="tab"]',$p).removeClass('disabled');
        }else{
            $js_goods_count_part.addClass('hide');
            $('a[data-toggle="tab"]',$p).addClass('disabled');
        }
        $js_type_goods_div.addClass('hide');
        $(".js_type_goods_"+type).removeClass('hide');
        $("#goods_select_type").val(type);
    }
    function typegood(obj){
        var $type=$("#goods_select_type"),type=obj.data("type"),
        $js_type_goods_div=$(".js_type_goods_div");
        $js_type_goods_div.addClass('hide');
        $(".js_type_goods_"+type).removeClass('hide');
        $("#goods_select_type").val(obj.val());
    }
   
})
